import { Ast, createRootAst } from "./ast";
import { CoreVisitor } from "./core";
import { getSafeClip, httpPost } from "./utils";
export interface RoomLink {
    link?: string;
    status?: `finding` | 'copy' | 'share' | 'open';
    name?: string;
    obj?: object;
}
export function getLiveRoomLink(time: number = 50) {
    try {
        const ctx: RoomLink = {
            status: `finding`
        }
        const visitor = new LiveRoomLinkVisitor()
        while (ctx.status !== `copy` && time > 0) {
            const root = createRootAst()
            root.visit(visitor, ctx)
            sleep(2000)
            time -= 1;
        }
        const link = getSafeClip()
        if (!link) {
            toastLog(`获取分享链接失败, ${device.sdkInt} ${link}`)
            return;
        }
        toastLog(`获取分享链接成功`)
        const res = link.match(/(http.*?) (.*?)的直播/)
        if (res && res.length > 0) {
            const [, link, name] = res;
            ctx.link = link;
            ctx.name = name;
            const obj = getRealLink(link)
            ctx.obj = obj;
        }
        return ctx;
    } catch (e) {
        return;
    } finally { }
}

export function getRealLink(link: string) {
    try {
        return httpPost('leader/getUserId', { url: link })
    } catch (e) {
        return {}
    }
}
export class LiveRoomLinkVisitor extends CoreVisitor {
    visitRelativeLayout(node: Ast, ctx: RoomLink) {
        if (!node.visibleToUser) return;
        if (node.id.endsWith('live_bottom_bar_single_icon_container')) {
            if (ctx.status !== 'open') {
                node.click()
                ctx.status = `open`
                sleep(1000)
                return;
            }
        }
        node.children.map(child => child.visit(this, ctx))
    }

    visitFrameLayout(node: Ast, ctx: RoomLink) {
        if (!node.visibleToUser) return;
        if (node.id.endsWith('live_play_root_container')) {
            const nodes = node.children.filter(it => it.className === `android.widget.RelativeLayout` && it.id.endsWith(`live_bottom_bar_single_icon_container`))
            if (nodes.length === 2) {
                nodes[0].click()
                sleep(1000)
                ctx.status = `open`
                return;
            }
        }
        node.children.map(child => child.visit(this, ctx))
    }


    visitTextView(node: Ast, ctx: RoomLink) {
        if (node.id.endsWith(`live_bottom_bar_item_title_text_view`) && node.text === `分享`) {
            node.parent.click()
            sleep(1000)
            ctx.status = `share`
            return;
        }
        if (node.text === `复制链接` && node.id.endsWith(`title`)) {
            node.parent.click()
            sleep(1000)
            ctx.status = `copy`
            return;
        }
    }

    visitImageView(node: Ast, ctx: RoomLink) {
        if (node.id.endsWith(`live_red_packet_container_close_view`)) {
            node.click()
            sleep(1000)
            return;
        }
        if (node.id.endsWith(`close_view`)) {
            node.click()
            sleep(1000)
            return;
        }
    }
}